package com.wy.webiter;

import com.wy.dao.ConsumerDao;
import com.wy.form.ConsumerForm;
import com.wy.tool.Chinese;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author Administrator
 * 
 */
public class ConsumerServlet extends HttpServlet {
	private ConsumerDao consumerDao = null;

	private int method;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		method = Integer.parseInt(request.getParameter("method"));
		if (method == 0) {
			checkConsumer(request, response); // 用户登录
		}
		if (method == 1) {
			registerConsumer(request, response);// 用户注册
		}
		if (method == 2) {
			queryConsumerForm(request, response);// 后台操作中，对一个用户进行查询
		}
		if (method == 3) {
			deleteConsumerForm(request, response);// 后台操作中，对用户进行删除操作
		}
		if (method == 4) {
			queryConsumerHostForm(request, response); // 后台操作中，对博主的查询操作
		}
		if (method == 5) {
			updateConsumerHostForm(request, response); // 后台操作中，对博主信息的修改操作
		}
		if (method == 6) {
			front_updateConsumerForm(request, response); // ǰ前台操作中，用户对登陆进行修改
		}

	}

	// 前台操作中，用户登陆进行修改
	public void front_updateConsumerForm(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		ConsumerForm form = new ConsumerForm();
		consumerDao = new ConsumerDao();
		form.setAccount(Chinese.toChinese(request.getParameter("account")));
		form.setPassword(Chinese.toChinese(request.getParameter("password")));
		form.setName(Chinese.toChinese(request.getParameter("name")));
		form.setSex(Chinese.toChinese(request.getParameter("sex")));
		form.setQQNumber(request.getParameter("QQnumber"));
		form.setMainPage(request.getParameter("mainPage"));
		form.setInterest(Chinese.toChinese(request.getParameter("interest")));
//		form.setId(Integer.valueOf(request.getParameter("id")));
		form.setEMail(request.getParameter("eMail"));
		if (consumerDao.front_updateConsumerForm(form)) {
			out.print("<script language=javascript>alert('修改用户成功，请重新登陆！');window.location.href='dealwith.jsp?sign=2';</script>");
		} else {
			out.print("<script language=javascript>alert('修改用户信息失败');history.go(-1);</script>");
		}

	}

	// 对博主信息的修改
	public void updateConsumerHostForm(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		ConsumerForm form = new ConsumerForm();
		consumerDao = new ConsumerDao();
		form.setAccount(Chinese.toChinese(request.getParameter("account")));
		form.setPassword(Chinese.toChinese(request.getParameter("password")));
		form.setName(Chinese.toChinese(request.getParameter("name")));
		form.setSex(Chinese.toChinese(request.getParameter("sex")));
		form.setQQNumber(request.getParameter("QQnumber"));
		form.setMainPage(request.getParameter("mainPage"));
		form.setInterest(Chinese.toChinese(request.getParameter("interest")));
		form.setEMail(request.getParameter("eMail"));
		form.setManageLevel("高级");
		String result = "修改博主信息失败！";

		if (consumerDao.updateConsumerForm(form)) {
			result = "修改博主信息失败！";
		}

		request.setAttribute("result", result);
		request.setAttribute("form", consumerDao.getConsumerForm(form
				.getAccount()));
		consumerDao.closeConnection();
		RequestDispatcher requestDispatcher = request
				.getRequestDispatcher("back_consumerSelectHostForm.jsp");
		requestDispatcher.forward(request, response);
	}

	// 后台操作中，用户进行删除操作
	public void deleteConsumerForm(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=GBK");
		String account = Chinese.toChinese(request.getParameter("account"));
		consumerDao = new ConsumerDao();
		PrintWriter out = response.getWriter();
		if (consumerDao.deleteConsumerForm(account)) {
			out.print("<script language=javascript>alert('删除用户成功，请重新进行查询！');window.location.href='back_consumerSelect.jsp';</script>");
		} else {
			out.print("<script language=javascript>alert('删除用户信息失败！');history.go(-1);</script>");
		}
		consumerDao.closeConnection();
	}

	// �后台，对博主的查询操作
	public void queryConsumerHostForm(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		consumerDao = new ConsumerDao();
		request.setAttribute("form", consumerDao.getConsumerForm("mr"));
		consumerDao.closeConnection();
		RequestDispatcher requestDispatcher = request
				.getRequestDispatcher("back_consumerSelectHostForm.jsp");
		requestDispatcher.forward(request, response);

	}


	// 后台对一个用户查询
	public void queryConsumerForm(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		consumerDao = new ConsumerDao();
		String account = Chinese.toChinese(request.getParameter("account"));
		request.setAttribute("form", consumerDao.getConsumerForm(account));
		consumerDao.closeConnection();
		RequestDispatcher requestDispatcher = request
				.getRequestDispatcher("back_consumerSelectForm.jsp");
		requestDispatcher.forward(request, response);
	}

	// 用户注册操作
	public void registerConsumer(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		ConsumerForm form = new ConsumerForm();
		consumerDao = new ConsumerDao();

		form.setAccount(request.getParameter("account"));
		form.setPassword(Chinese.toChinese(request.getParameter("password")));
		form.setName(Chinese.toChinese(request.getParameter("name")));
		form.setSex(Chinese.toChinese(request.getParameter("sex")));
		form.setQQNumber(request.getParameter("QQnumber"));
		form.setMainPage(request.getParameter("mainPage"));
		form.setInterest(Chinese.toChinese(request.getParameter("interest")));
		form.setEMail(request.getParameter("eMail"));
		form.setManageLevel("普通");
		String result = "fail";

		if (consumerDao.getConsumerForm(form.getAccount()) == null) {
			if (consumerDao.addConsumerForm(form)) {
				request.setAttribute("form", consumerDao.getConsumerForm(form.getAccount()));
				  result = "success";
					System.out.println(result);

			}
		}
		System.out.println(result);
		request.setAttribute("result", result);
		RequestDispatcher requestDispatcher = request.getRequestDispatcher("dealwith.jsp");
		requestDispatcher.forward(request, response);
	}

	// 用户登陆操作
	public void checkConsumer(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String account = request.getParameter("account");
		consumerDao = new ConsumerDao();
		//验证是否有此用户
		ConsumerForm consumerForm = consumerDao.getConsumerForm(account);

		//ConsumerDao返回consumerForm并检验密码是否一样
		if (consumerForm == null) {
			request.setAttribute("information", "您输入的用户名不存在，请重新输入！");
		} else if (!consumerForm.getPassword().equals(
				request.getParameter("password"))) {
			request.setAttribute("information", "您输入的登陆密码有误，请重新输入！");
		} else {

			request.setAttribute("form", consumerForm);
		}
		RequestDispatcher requestDispatcher = request.getRequestDispatcher("dealwith.jsp");
		requestDispatcher.forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}
